

from .paper import OpenReviewPaper
from .load_from_tis import LoadPaperFromTisHandle
from .load_from_tis import LoadPaperFromTisHandleAtIdx
from .load_from_tis import CountPaperInTisHandle

class Filter:

    def __init__(self,papers:list[OpenReviewPaper]):
        self.papers = papers
        self.length = len(self.papers)
    
    def filter(self,filter_func):
        print(f"filter-ok! before count:{self.length}")
        selected_papers = []
        for paper in self.papers:
            if filter_func(paper):
                selected_papers.append(paper)
        print(f"filter-ok! after count:{len(selected_papers)}")
        return Filter(selected_papers)
    
    def dump(self):
        return self.papers


def get_filter_func_KeyWordsMatch(keyword:str):
    kw = str(keyword)
    def KeyWordsMatch(paper:OpenReviewPaper):
        nonlocal kw
        keep = False
        # 混合搜索
        content = paper.get_comment() + " " + \
                paper.get_summary() + " " + \
                paper.get_title() + " " + \
                str(paper.get_keywords) + " "
        content = content.lower()
        if kw.lower() in content:
            keep = True
        return keep
    return KeyWordsMatch


def get_filter_func_RatingUpTo(up_to_score:int):
    ut_score = int(up_to_score)
    def RatingUpTo(paper:OpenReviewPaper):
        nonlocal ut_score
        keep = False
        for review in paper.get_views():
            score = review.TryGetRatingScore("rating")
            if isinstance(score,int):
                if score >= ut_score:
                    keep = True
        return keep
    return RatingUpTo





